package pay

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/service"
	"github.com/maxyang107/jinglan/utils"
)

/**
 * @description: 方法描述：支付宝支付
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func AliPay(c *gin.Context) {
	var pay common.Detail
	if err := c.ShouldBind(&pay); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid_, exists := c.Get("UserId")
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	pay.UserId = utils.InterfaceToUser(userid_)
	s, err := service.PayService.DealPay(pay)
	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}
	common.ResponseDetailToJson(http.StatusOK, s, "success", c)
}

func RePay(c *gin.Context) {
	// var pay common.Detail
	// if err := c.ShouldBind(&pay); err != nil {
	// 	common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
	// 	return
	// }
	// userid_, exists := c.Get("UserId")
	// if !exists {
	// 	common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
	// 	return
	// }

	// pay.UserId = utils.InterfaceToUser(userid_)
	// s, err := service.PayService.DealRePay(pay)
}

func AliNotify(c *gin.Context) {
	c.Request.ParseForm()
	err := service.PayService.DealAliNotify(c)

	if err != nil {
		fmt.Println(err.Error())
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}

	common.ResponseToJson(http.StatusOK, common.Ok, "success", c)
}
